package com.diankangwo.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.diankangwo.dao.OnlineRefundLogDAO;
import com.diankangwo.domain.MapOptional;
import com.diankangwo.domain.OnlineRefundLog;
import com.diankangwo.domain.OnlineTransferData;
import com.diankangwo.dto.MapOptionalDTO;
import com.diankangwo.dto.OnlineRefundLogDTO;
import com.diankangwo.dto.OnlineTransferDataDTO;
import com.diankangwo.service.OnlineRefundLogService;
import com.diankangwo.util.PageSupport;

@Service
public class OnlineRefundLogServiceImpl implements OnlineRefundLogService {

	@Autowired
	private OnlineRefundLogDAO onlineRefundLogDAO;

	@Override
	public boolean insertLog(OnlineRefundLogDTO logDTO) {
		try {
			OnlineRefundLog log = this.dtoToDomain(logDTO);
			onlineRefundLogDAO.insert(log);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	@Override
	public boolean updateLog(OnlineRefundLogDTO logDTO) {
		try {
			OnlineRefundLog log = this.dtoToDomain(logDTO);
			onlineRefundLogDAO.update(log);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	@Override
	public boolean saveOrUpdate(OnlineRefundLogDTO logDTO) {
		try {
			OnlineRefundLog log = this.dtoToDomain(logDTO);
			onlineRefundLogDAO.saveOrUpdate(log);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public OnlineRefundLogDTO searchLogById(Serializable id) {
		try {
			OnlineRefundLog log = onlineRefundLogDAO.find(id);
			OnlineRefundLogDTO logDTO = this.domainToDto(log);
			return logDTO;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	@Override
	public OnlineRefundLogDTO searchLogByRefundNo(String refundNo) {
		try {
			OnlineRefundLog log = onlineRefundLogDAO.findByProperty("refundNo", refundNo).get(0);
			OnlineRefundLogDTO logDTO = this.domainToDto(log);
			return logDTO;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public List<OnlineRefundLogDTO> searchAllLog() {
		try {
			List<OnlineRefundLog> orlList = onlineRefundLogDAO.findAll();
			List<OnlineRefundLogDTO> orfdList = new ArrayList<OnlineRefundLogDTO>();
			for (OnlineRefundLog orl : orlList) {
				OnlineRefundLogDTO orld = new OnlineRefundLogDTO();
				orld = this.domainToDto(orl);
				orfdList.add(orld);
			}
			return orfdList;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public PageSupport<OnlineRefundLogDTO> searchLogByPage(Integer pageNo, Integer pageSize) {
		try {
			PageSupport<OnlineRefundLogDTO> psorld = new PageSupport<OnlineRefundLogDTO>();
			PageSupport<OnlineRefundLog> psorl = onlineRefundLogDAO.findPagination(pageNo, pageSize);
			List<OnlineRefundLog> orlList = psorl.getResultData();
			List<OnlineRefundLogDTO> orld = new ArrayList<OnlineRefundLogDTO>();
			for (OnlineRefundLog log : orlList) {
				OnlineRefundLogDTO ord = new OnlineRefundLogDTO();
				ord = this.domainToDto(log);
				orld.add(ord);
			}
			psorld.setResultData(orld);
			psorld.setStart(psorl.getStart());
			return psorld;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}
	
	/**
	 *  转换实体
	 * @param logDTO
	 * @return
	 */
	public OnlineRefundLog dtoToDomain(OnlineRefundLogDTO logDTO){
		OnlineRefundLog log = new OnlineRefundLog();
		log.setIdKey(UUID.randomUUID().toString());
		log.setBillNo(logDTO.getBillNo());
		log.setRefundNo(logDTO.getRefundNo());
		log.setBatchNo(logDTO.getBatchNo());
		log.setRefundFee(logDTO.getRefundFee());
		log.setAccountName(logDTO.getAccountName());
		if(null != logDTO.getTransferDatalist() && logDTO.getTransferDatalist().size() != 0){
			List<OnlineTransferData> otfdList = new ArrayList<OnlineTransferData>();
			for (OnlineTransferDataDTO otdd : logDTO.getTransferDatalist()) {
				OnlineTransferData otfd = new OnlineTransferData();
				otfd.setTransferId(otdd.getTransferId());
				otfd.setReceiverAccount(otdd.getReceiverAccount());
				otfd.setReceiverName(otdd.getReceiverName());
				otfd.setTransferFee(otdd.getTransferFee());
				otfd.setTransferNote(otdd.getTransferNote());
				otfdList.add(otfd);
			}
			log.setList(otfdList);
		}
		if(null != logDTO.getOptionalList() && logDTO.getOptionalList().size() != 0){
			List<MapOptional> omoList = new ArrayList<MapOptional>();
			for (MapOptionalDTO mod : logDTO.getOptionalList()) {
				MapOptional mo = new MapOptional();
				mo.setIdKey(UUID.randomUUID().toString());
				mo.setName(mod.getName());
				mo.setValue(mod.getValue());
			}
			log.setOptionalList(omoList);
		}
		log.setRefundstatus(logDTO.getRefundStatus());
		log.setWebhookStatus(logDTO.getWebhookStatus());
		log.setUserRefundStatus(logDTO.getUserRefundStatus());
		log.setCreateDate(logDTO.getCreateDate());
		log.setCreateUser(logDTO.getCreateUser());
		log.setUpdateDate(logDTO.getUpdateDate());
		log.setUpdateUser(logDTO.getUpdateUser());
		return log;
	}

	/**
	 *  实体转换
	 * @param log
	 * @return
	 */
	public OnlineRefundLogDTO domainToDto(OnlineRefundLog log){
		OnlineRefundLogDTO logDTO = new OnlineRefundLogDTO();
		logDTO.setId(log.getId());
		logDTO.setIdKey(log.getIdKey());
		logDTO.setBillNo(log.getBatchNo());
		logDTO.setRefundNo(log.getRefundNo());
		logDTO.setBatchNo(log.getBatchNo());
		logDTO.setRefundFee(log.getRefundFee());
		logDTO.setAccountName(log.getAccountName());
		if(null != log.getList() && log.getList().size() != 0){
			List<OnlineTransferDataDTO> otfddList = new ArrayList<OnlineTransferDataDTO>();
			for (OnlineTransferData otfd : log.getList()) {
				OnlineTransferDataDTO otfdd = new OnlineTransferDataDTO();
				otfdd.setId(otfd.getId());
				otfdd.setTransferId(otfd.getTransferId());
				otfdd.setReceiverAccount(otfd.getReceiverAccount());
				otfdd.setReceiverName(otfd.getReceiverName());
				otfdd.setTransferFee(otfd.getTransferFee());
				otfdd.setTransferNote(otfd.getTransferNote());
				otfddList.add(otfdd);
			}
			logDTO.setTransferDatalist(otfddList);
		}
		if(null != log.getOptionalList() && log.getOptionalList().size() != 0){
			List<MapOptionalDTO> modList = new ArrayList<MapOptionalDTO>();
			for(MapOptional mo : log.getOptionalList()){
				MapOptionalDTO mod = new MapOptionalDTO();
				mod.setId(mo.getId());
				mod.setName(mo.getName());
				mod.setValue(mo.getValue());
				modList.add(mod);
			}
			logDTO.setOptionalList(modList);
		}
		logDTO.setRefundStatus(log.getRefundstatus());
		logDTO.setCreateDate(log.getCreateDate());
		logDTO.setCreateUser(log.getCreateUser());
		logDTO.setUpdateDate(log.getUpdateDate());
		logDTO.setUpdateUser(log.getUpdateUser());
		return logDTO;
	}

	
}
